﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CQIE.OVS.BLL;
using CQIE.OVS.Model;
using CQIE.OVS.Web.Models;
using NHibernate.Criterion;

namespace CQIE.OVS.Web.Controllers
{
    public class ActiveController : Controller
    {
        [HttpGet]
        public ActionResult CreateActive()
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateActive(string name)
        {

            return RedirectToAction("ActiveManager");
        }

        public JsonResult CountHot(int Cid)
        {
            var Cam = new CampaignerBLL().Get(Cid);
            
            int all = GetAllTickets(Cam.Activity);
            var data = new VoteTemp()
            {
                Hot = ((float)Cam.Num / all).ToString("P"),
                ID = Cam.ID,
                Num = Cam.Num
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        private int GetAllTickets(Activity model)
        {
            int AllCount = 0;
            foreach (Campaigner item in model.Campaigners)
            {
                AllCount = AllCount + item.Num;
            }

            return AllCount;
        }

        public JsonResult GetCamTable(int AID,int page = 1, int limit =10)
        {
            ActivityBLL bll = new ActivityBLL();
            var model = bll.Get(AID);
            CampaignerBLL cbll = new CampaignerBLL();
            IList<ICriterion> q = new List<ICriterion>();
            q.Add(Expression.Gt("IsAlive",0));
            q.Add(Expression.Eq("Activity",model));
            IList<Order> listoOrders = new List<Order>();
            listoOrders.Add(new Order("Num",false));
            int all = 0;
            var list = cbll.GetPaged(q, listoOrders, page, limit, out all);
            var data = new TableModel<CamModel>()
            {
                status = 0,
                message = "",
                total = all,
                data = list.Select(n=>new CamModel(){
                    Name = n.Name,
                    Sex = n.Sex==1?"男":"女",
                    Num = n.Num
                })
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetResult(int Aid)
        {
            ActivityBLL bll = new ActivityBLL();
            var model = bll.Get(Aid);
            ViewBag.Active = model.ID;
            CampaignerBLL cbll =new CampaignerBLL();
            IList<QueryConditions> query = new List<QueryConditions>();
            query.Add(new QueryConditions("IsAlive",">",0));
            query.Add(new QueryConditions("Activity","=",model));
            var list = cbll.Find(query);
            return View(list);
        }

        public ActionResult DetailActive(int Id = 12)
        {
            ActivityBLL bll = new ActivityBLL();
            Activity model = bll.Get(Id);
            return View(model);
        }

        [HttpGet]
        public ActionResult EditActive(int Id=1)
        {
            ActivityBLL bll = new ActivityBLL();
            Activity model = bll.Get(Id);
            return View(model);
        }

        public string EditActive(Activity model)
        {
            ActivityBLL bll =new ActivityBLL();
            Activity ac2 = bll.Get(model.ID);
            model.Campaigners = ac2.Campaigners;
            model.IsAlive = 1;
            bll.Update(model);
            //  layui.use('layer', function () { layer = layui.layer;  layer.closeAll('iframe');} );
            return string.Empty;
        }

        public ActionResult ActiveManager(string searchName = "", int pageNum = 1, int pageSize = 10)
        {
            return View();
        }

        
        /// <summary>
        /// 获取活动的参赛人员
        /// </summary>
        /// <param name="Aid">活动ID</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetActiveCam(int Aid, int page = 1, int limit = 10)
        {
            ActivityBLL bll = new ActivityBLL();
            CampaignerBLL cambll = new CampaignerBLL();
            Activity Active = bll.Get(Aid);
            IList<QueryConditions> querys = new List<QueryConditions>();
            querys.Add(new QueryConditions("IsAlive", "!=", 0));
            querys.Add(new QueryConditions("Activity", "=", Active));
            int all;
            IEnumerable<CamModel> list 
                = cambll.GetPaged(querys,page, limit, out all).Select(n => new CamModel()
            {
                Id=n.ID,
                Age = n.GetAge(n.Birthday),
                Info = n.Info,
                Name = n.Name,
                Sex = n.Sex == 0 ? "女" : "男"
            });
            //用的CamModel
            var dataOrder = new TableModel<CamModel>()
            {
                data = list,
                message = "",
                status = 0,
                total = list.Count()
            };

            return Json(dataOrder, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取管理界面列表
        /// </summary>
        /// <param name="searchName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetList(string searchName = "", int page = 1, int limit = 10)
        {
            ViewBag.SearchName = searchName;
            IList<QueryConditions> querys = new List<QueryConditions>();
            querys.Add(new QueryConditions("IsAlive", ">", 0));
            querys.Add(new QueryConditions("Title", "like", string.Format("%{0}%", searchName)));
            ActivityBLL bll = new ActivityBLL();
            int all = 0;
            var list = bll.GetPaged(querys, page, limit, out all);

            Func<DateTime, DateTime, string> getstate = (stime,etime) =>
            {
                if (DateTime.Now < stime)
                {
                    return "未开始";
                }
                
                if (DateTime.Now >= stime && DateTime.Now <= etime)
                {
                    return "进行中";
                }

                if (DateTime.Now > etime)
                {
                    return "已结束";
                }

                return "未知";
            };

            var data = list.Select(n => new Active()
            {
                Id = n.ID,
                Title = n.Title,
                Info = n.Info,
                IsAlive = n.IsAlive,
                CamNum = n.Campaigners.Where(cc=>cc.IsAlive>0).ToList().Count(),
                State = getstate(n.DateStart,n.DateEnd)
            });

            var dataOrder = new TableModel<Active>()
            {
                data = data,
                message = "",
                status = 0,
                total = all
            };
            return Json(dataOrder, JsonRequestBehavior.AllowGet);
        }

        public string SaveActive(Active data1)
        {

            ActivityBLL activebll = new ActivityBLL();
            CampaignerBLL cmBll = new CampaignerBLL();
            Activity active = new Activity()
            {
                Info = data1.Info,
                Title = data1.Title,
                DateEnd = data1.Edtime,
                DateStart = data1.Sttime,
                IsAlive = 1
            };
            activebll.Create(active);

            List<Campaigner> list = new List<Campaigner>();
            foreach (CamModel model in data1.List)
            {
                Campaigner cam = new Campaigner()
                {
                    Activity = active,
                    Age = model.Age,
                    Name = model.Name,
                    Info = model.Info,
                    Sex = model.Sex == "男" ? 1 : 0,
                    IsAlive = 1
                };
                cam.Birthday = cam.GetBirthday(cam.Age);
                cmBll.Create(cam);
                list.Add(cam);
            }

            active.Campaigners = list;
            activebll.Update(active);
            return "保存成功！";
        }

        public string DeleteActive(int Id)
        {
            ActivityBLL bll = new ActivityBLL();
            Activity model = bll.Get(Id);
            model.IsAlive = 0;
            bll.Update(model);
            return string.Empty;
        }
    }
}